<?php

namespace app\api\controller;

use app\api\service\shop\OrderService;
use app\api\validate\OrderValidate;

/**
 * 订单控制器
 */
class Order extends Base
{

    protected $noNeedRight = ['getSellOrderInfoList', 'setOrderExpire', 'getTemplateMoney', 'getOrderInfo', 'getOrderInfoList', 'cancelOrder', 'createOrder', 'buyOrder', 'getOrderMoney', 'getSellOrderInfo'];

    private OrderService $orderService;

    public function __construct(OrderService $orderService, OrderValidate $validate)
    {
        parent::__construct();
        $this->orderService = $orderService;
        $this->validate = $validate;
    }

    /**
     * 获取订单信息
     */
    public function getOrderInfo()
    {
        $data = $this->request->get();
        $this->valid($data, 'order');
        $userId = intval($this->auth->id);
        $companyId = intval($this->auth->company_id);
        $type = true; // 目前先写死为公司访问
        $data = $this->orderService->getorderInfo($userId, $companyId, $type, $data['orderId']);
        empty($data) ? $this->error('当前订单不存在') : $this->success('ok', $data);
    }

    /**
     * 取消订单
     */
    public function cancelOrder()
    {

        $data = $this->request->post();
        $this->valid($data, 'order');
        $userId = intval($this->auth->id);
        $this->antiShake($userId, $data['orderId'], 5, null, '请不要在短时间内重复点击取消订单, 请五秒后再试');
        $companyId = intval($this->auth->company_id);
        $type = true; // 目前先写死为公司访问
        $data = $this->orderService->cancelOrder($userId, $companyId, $type, $data['orderId']);
        empty($data) ? $this->success('取消成功') : $this->error($data);
    }

    /**
     * 支付订单
     */
    public function buyOrder()
    {
        $data = $this->request->post();
        $this->valid($data, 'order');
        $userId = intval($this->auth->id);
        $this->antiShake($userId, $data['orderId'], 5, null, '请不要在短时间内重复点击支付订单, 请五秒后再试');
        $companyId = intval($this->auth->company_id);
        $qdUserId = intval($this->auth->qd_user_id);
        $type = true; // 目前先写死为公司访问
        $data = $this->orderService->buyOrder($userId, $qdUserId, $companyId, $type, $data['orderId']);
        empty($data) ? $this->success('支付成功') : $this->error($data);
    }

    /**
     * 创建订单
     */
    public function createOrder()
    {
        $data = $this->request->post();
        $this->valid($data, 'buy');
        $userId = intval($this->auth->id);
        $this->antiShake($userId, $data['id'], 5, null, '请不要在短时间内重复点击创建订单, 请五秒后再试');
        $company_id = intval($this->auth->company_id);
        $type = true; // 目前先写死为公司访问
        $orderId = generateOrderId('T');
        $data = $this->orderService->createOrder($data['id'], $userId, $company_id, $type, $orderId);
        if (empty($data)) {
            $this->success('创建成功', $orderId);
        }
        if (is_array($data)) $this->error($data['message'], $data['sn'], -400);
        $this->error($data);
    }

    /**
     * 获取订单价格
     */
    public function getOrderMoney()
    {
        $data = $this->request->get();
        $this->valid($data, 'order');
        $userId = intval($this->auth->id);
        $companyId = intval($this->auth->company_id);
        $type = true; // 目前先写死为公司访问
        $data = $this->orderService->getOrderMoney($data['orderId'], $userId, $companyId, $type);
        empty($data) ? $this->error('该用户当前订单不存在') : $this->success('ok', $data);
    }

    /**
     * 集中设置过期命令
     */
    public function setOrderExpire()
    {
        $this->orderService->setOrderExpire();
    }

    /**
     * 获取我出售的订单列表
     */
    public function getSellOrderInfoList()
    {
        $data = $this->request->get();
        $this->valid($data, 'list');
        $userId = intval($this->auth->id);
        $type = true; // 目前先写死为公司访问
        $data = $this->orderService->getSellOrderInfoList($userId, $type, $data['page'], $data['limit'], $data['resource']);
        $this->success('ok', $data);
    }

    /**
     * 获取订单信息列表
     */
    public function getOrderInfoList()
    {
        $data = $this->request->get();
        $this->valid($data, 'list');
        $userId = intval($this->auth->id);
        $companyId = intval($this->auth->company_id);
        $type = true; // 目前先写死为公司访问
        $data = $this->orderService->getorderInfoList($userId, $companyId, $type, $data['page'], $data['limit'], $data['resource']);

        foreach ($data['row'] as $key => &$value) {
            $order = $value->toArray();
            $order['factor_num'] = $order['factors_array'] ? count($order['factors_array']) : 0;
            $value = $order;
        }
        unset($value);
        $this->success('ok', $data);
    }

    /**
     * 获取出售的订单信息
     */
    public function getSellOrderInfo()
    {
        $data = $this->request->get();
        $this->valid($data, 'sell');
        $userId = intval($this->auth->id);
        $data = $this->orderService->getSellOrderInfo($userId, $data['type'], $data['orderId']);
        empty($data) ? $this->error('当前订单不存在') : $this->success('ok', $data);
    }


}